package com.zb.kc.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.kc.commonutils.R;
import com.zb.kc.entity.EduTeacher;
import com.zb.kc.entity.vo.TeacherQuery;
import com.zb.kc.service.IEduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wanggen
 * @since 2020-04-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
public class EduTeacherController {

    @Autowired
    private IEduTeacherService eduTeacherService;

    // 查询讲师表所有的数据
    @GetMapping("findAll")
    public R findAllTeacher() {
        return R.ok().data("items", eduTeacherService.list());
    }

    @GetMapping("{id}")
    public R findById(@PathVariable("id") String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation("逻辑删除讲师，不会真正删除数据库数据")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        return eduTeacherService.removeById(id) ? R.ok().message("删除讲师成功") : R.err().message("删除讲师失败");
    }

    /**
     * 分页查询讲师的方法
     *
     * @return
     */
    @ApiOperation("分页查询讲师测试")
    @GetMapping("pageTeacher/{current}/{size}")
    public R pageTeacher(
            @ApiParam("当前页码") @PathVariable("current") Long current,
            @ApiParam("每页显示尺寸") @PathVariable("size") Long size) {
        // 创建分页的对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, size);
        Page<EduTeacher> page = eduTeacherService.page(eduTeacherPage, null);
        return R.ok().dataPage(page);
    }

    /**
     * 根据条件查询带分页的方法
     *
     * @param current
     * @param size
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "条件分页查询讲师数据")
    @GetMapping("pageTeacherCondition/{current}/{size}")
    public R pageTeacherCondition(
            @ApiParam(value = "当前页码", required = true) @PathVariable(name = "current") Long current,
            @ApiParam(value = "每页显示尺寸", required = true) @PathVariable(name = "size") Long size, TeacherQuery teacherQuery) {
        // 创建配置对象
        Page<EduTeacher> pageTeacher = new Page<>(current, size);
        // 调用方法实现条件查询分页
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 配置查询条件 动态sql
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            wrapper.like("name", teacherQuery.getName());
        }
        if (!StringUtils.isEmpty(teacherQuery.getLevel())) {
            wrapper.eq("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            wrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            wrapper.le("gmt_create", teacherQuery.getEnd());
        }
        Page<EduTeacher> page = eduTeacherService.page(pageTeacher, wrapper);
        // 返回分页对象
        return R.ok().dataPage(page);
    }

    /**
     * 添加讲师
     *
     * @param eduTeacher
     * @return
     */
    @PostMapping
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok().message("添加讲师成功") : R.err().message("添加失败");
    }

    @PutMapping
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        QueryWrapper updateWrapper = new QueryWrapper();
        updateWrapper.eq("id", eduTeacher.getId());
        boolean update = eduTeacherService.update(eduTeacher, updateWrapper);
        return update ? R.ok().message("修改成功") : R.err().message("修改失败");
    }

}
